package org.gangcai.mac.shop.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.activity.BrandActivity;
import org.gangcai.mac.shop.activity.SPProductListActivity;
import org.gangcai.mac.shop.activity.ShopDetailActivity;
import org.gangcai.mac.shop.activity.TTShopDetailActivity;
import org.gangcai.mac.shop.adapter.AnimationAdapter;
import org.gangcai.mac.shop.adapter.AnimationAdapter2;
import org.gangcai.mac.shop.adapter.HeaderAndFooterAdapter;
import org.gangcai.mac.shop.api.AppAPI;
import org.gangcai.mac.shop.bean.CategoryBean;
import org.gangcai.mac.shop.bean.HomeDataBean;
import org.gangcai.mac.shop.bean.HomeMainDataBean;
import org.gangcai.mac.shop.bean.PostsArticleBaseBean;
import org.gangcai.mac.shop.bean.UpdateShopData;
import org.gangcai.mac.shop.manager.RetrofitManageres;
import org.gangcai.mac.shop.manager.RetrofitManagers;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.rxlife.RxFragment;
import org.gangcai.mac.shop.utils.PromptDialogUtils;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import me.leefeng.promptlibrary.PromptDialog;

/**
 * 商品界面
 */
public class CategoryViewFragment extends RxFragment implements MultiItemTypeAdapter.OnItemClickListener, OnRefreshListener {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    Unbinder unbinder;
    @BindView(R.id.recyclerview_category)
    RecyclerView recyclerview_category;
    @BindView(R.id.category_left)
    TextView category_left;
    @BindView(R.id.category_right)
    TextView category_right;
    ArrayList<CategoryBean.CateBean> datalist = new ArrayList<>();
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String mParam1;
    private String mParam2;
    private CommonAdapter<CategoryBean.CateBean> adapter;
//    private CommonAdapter<CategoryBean.ChildrenBean> adapter1;
//    private HeaderAndFooterAdapter headerAndFooterAdapter;
    private PromptDialog prompDialog;
    private AnimationAdapter2 animationAdapter2;
    private String cid="3";

    public CategoryViewFragment() {
    }

    public static CategoryViewFragment newInstance(String param1, String param2) {
        CategoryViewFragment fragment = new CategoryViewFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category_view, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        prompDialog = PromptDialogUtils.getPrompDialog(getActivity());
        recyclerview_category.setLayoutManager(new LinearLayoutManager(getActivity()));
        category_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cid="3";
                loadData(cid);
                category_left.setTextColor( getResources().getColor(R.color.home_title_color) );
                category_left.setBackgroundResource( R.drawable.bottom_solid_view );
                category_right.setTextColor(getResources().getColor(R.color.textColorDark));
                category_right.setBackgroundColor( getResources().getColor(R.color.white) );
            }
        });
        category_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cid="4";
                loadData(cid);
                category_left.setTextColor(getResources().getColor(R.color.textColorDark));
                category_left.setBackgroundColor(getResources().getColor(R.color.white) );
                category_right.setTextColor( getResources().getColor(R.color.home_title_color) );
                category_right.setBackgroundResource( R.drawable.bottom_solid_view );
            }
        });
        loadData(cid);
    }

    private void loadData(String cid) {
        LogUtils.e("loadCategoryFragment");
        RetrofitManageres.getInstance().create(AppAPI.class).HomeData("Portal","list","indexJson",cid)
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<HomeDataBean>bindToLifecycle())
                .subscribe(new CommonObserver<HomeDataBean>(refreshLayout) {
                    @Override
                    public void onSuccess(HomeDataBean value) {
                        if (value.getCode() == 101) {
                            animationAdapter2 = new AnimationAdapter2(getActivity(),value.getReferer().getPosts());
                            animationAdapter2.openLoadAnimation();
                            animationAdapter2.setNotDoAnimationCount(3);
                            recyclerview_category.setLayoutManager(new GridLayoutManager(getActivity(), 1));
                            animationAdapter2.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                @Override
                                public void onLoadMoreRequested() {

                                }
                            });
                            animationAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    PostsArticleBaseBean postsArticleBaseBean=value.getReferer().getPosts().get( position );
                                    Bundle bundle = new Bundle();
                                    ActivityUtils.startActivity(bundle, getActivity(), TTShopDetailActivity.class);
                                }
                            });
                            recyclerview_category.setAdapter(animationAdapter2);
                        }
                    }
                });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("id", datalist.get(position).getId());
        ActivityUtils.startActivity(bundle, this.getActivity(), ShopDetailActivity.class);
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }

    @Subscribe
    public void onEvent(UpdateShopData data) {
        loadData(cid);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData(cid);
    }
}
